package br.edu.ufcg.splab.techniques.reduction.functions.similarity;

import br.edu.ufcg.splab.generator.TestCase;

public class EuclidianDistance implements DistanceFunction {
	private final char FILL_CHARACTER = (char)0;
	
	/**
	 * Retrieve the similarity (number of repeated elements) in two specified paths.
	 * 
	 * @param path1 first test case.
	 * @param path2 second test case.
	 * @return similarity's value between the two paths. 
	 */
	public double getSimilarity(TestCase path1, TestCase path2){
		return euclidianDistance(path1.toString(), path2.toString());
	}
	
	/**
     * Provide a string representation of the similarity function to be written.
     * @return The string representation of the similarity function.
     */
	public String getFunctionName(){
		return "Euc";
	}
	
	private double euclidianDistance(String firstTC, String secondTC){
		
		if(firstTC.length() > secondTC.length()){
			do{
				secondTC += FILL_CHARACTER;
			}while(secondTC.length() != firstTC.length());
		}else if(secondTC.length() > firstTC.length()){
				do{
					firstTC += FILL_CHARACTER;
				}while(firstTC.length() != secondTC.length());
		}
		
		double diffSum = 0;
		for (int i = 0; i < firstTC.length(); i++) {
			int firstCharValue = (int)firstTC.charAt(i);
			int secondCharValue = (int)secondTC.charAt(i);
			diffSum += Math.pow(secondCharValue - firstCharValue, 2);
		}
		double result = Math.sqrt(diffSum);
		return result;
	}
}
